package com.ruoyi.web.controller.module.attendance;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.attendance.AttendanceWorkShift;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.domain.module.survey.Survey;
import com.ruoyi.system.service.module.property.IPropertyService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.ruoyi.web.controller.common.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.attendance.IAttendanceWorkShiftService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 工作班次Controller
 *
 * @author ruoyi
 * @date 2022-11-08
 */
@Validated
@Api(value = "工作班次控制器", tags = {"工作班次管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/attendance/shift")
public class AttendanceWorkShiftController extends BaseController {

    private final IAttendanceWorkShiftService workShiftService;
    private final IPropertyService propertyService;
    /**
     * 查询工作班次列表
     */
    @ApiOperation("查询工作班次列表")
    @PreAuthorize("@ss.hasPermi('module:shift:list')")
    @GetMapping("/list")
    public TableDataInfo<AttendanceWorkShift> list(@Validated AttendanceWorkShift attendanceWorkShift) {
		SysUser sysUser = getUser();
        QueryWrapper<AttendanceWorkShift> qw = new QueryWrapper<>();
		//用户类型 0 - 系统管理员  1 - 平台管理员 2 - 物业管理员  3 - 小区管理员
		if(sysUser.getUserType() == 2){
			qw.eq("property_id",sysUser.getPropertyId());
			if (sysUser.getEstateId()!=null){
				qw.eq("estate_id",sysUser.getEstateId());
			}
		} else if(sysUser.getUserType() == 3){
			qw.eq("estate_id",sysUser.getEstateId());
		}else{
			if(attendanceWorkShift.getPropertyId() != null){
				qw.eq("property_id",attendanceWorkShift.getPropertyId());
			}
		}
        TableDataInfo<AttendanceWorkShift> td = PageUtils.buildDataInfo(workShiftService.page(PageUtils.buildPage(),qw));
        List<AttendanceWorkShift> list = td.getRows();
        for (AttendanceWorkShift list1:list){
            if (list1.getPropertyId()!=null){
                Property property=propertyService.findById(list1.getPropertyId());
                if (property!=null){
                    list1.setPropertyName(property.getPropertyName());
                }
            }
        }
        return td;
    }

    /**
     * 导出工作班次列表
     */
    @ApiOperation("导出工作班次列表")
    @PreAuthorize("@ss.hasPermi('module:shift:export')")
    @Log(title = "工作班次", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<AttendanceWorkShift> export(@Validated AttendanceWorkShift attendanceWorkShift) {
        List<AttendanceWorkShift> list = workShiftService.list();
        ExcelUtil<AttendanceWorkShift> util = new ExcelUtil<AttendanceWorkShift>(AttendanceWorkShift.class);
        return util.exportExcel(list, "工作班次");
    }

    /**
     * 获取工作班次详细信息
     */
    @ApiOperation("获取工作班次详细信息")
    @PreAuthorize("@ss.hasPermi('module:shift:query')")
    @GetMapping("/{id}")
    public AjaxResult<AttendanceWorkShift> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        AttendanceWorkShift attendanceWorkShift = workShiftService.getById(id);
        return AjaxResult.success(attendanceWorkShift);
    }

    /**
     * 获取工作班次详细信息
     */
    @ApiOperation("获取工作班次详细信息")
    @PreAuthorize("@ss.hasPermi('module:shift:query')")
    @GetMapping("/list/{id}")
    public TableDataInfo<AttendanceWorkShift> getTime(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        QueryWrapper<AttendanceWorkShift> qw = new QueryWrapper<>();
        if(id!=null){
            qw.eq("property_id",id);
        }
        TableDataInfo<AttendanceWorkShift> td = PageUtils.buildDataInfo(workShiftService.page(PageUtils.buildPage(),qw));
        List<AttendanceWorkShift> list = td.getRows();
        return td;
    }
    /**
     * 新增工作班次
     */
    @ApiOperation("新增工作班次")
    @PreAuthorize("@ss.hasPermi('module:shift:add')")
    @Log(title = "工作班次", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody AttendanceWorkShift attendanceWorkShift) {
        return toAjax(workShiftService.save(attendanceWorkShift) ? 1 : 0);
    }

    /**
     * 修改工作班次
     */
    @ApiOperation("修改工作班次")
    @PreAuthorize("@ss.hasPermi('module:shift:edit')")
    @Log(title = "工作班次", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody AttendanceWorkShift attendanceWorkShift) {
        return toAjax(workShiftService.updateById(attendanceWorkShift) ? 1 : 0);
    }

    /**
     * 删除工作班次
     */
    @ApiOperation("删除工作班次")
    @PreAuthorize("@ss.hasPermi('module:shift:remove')")
    @Log(title = "工作班次" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(workShiftService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
